package linuxbox.bookmark.logic;

import java.util.*;
import org.apache.log4j.Logger;

import linuxbox.bookmark.bean.*;
import linuxbox.util.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class BookmarkHome {
  protected static Logger logger = Logger.getLogger(BookmarkHome.class.getName());

  public final static String VT_ALL = "ALL";
  public final static String VT_SYSTEM = "SYSTEM";
  public final static String VT_MINE = "MINE";
  public static BookmarkHome staticInstance = null;

  public static BookmarkHome getInstance()
  {
    if (staticInstance == null)
    {
      staticInstance = new BookmarkHome();
    }
    return staticInstance;
  }
  private BookmarkHome() {}

  public BookmarkEntry add()
  {
    BookmarkBean bean = new BookmarkBean();
    try
    {
      bean.create();
    }
    catch(Exception e)
    {
      logger.error("Error create bookmark entry:", e);
    }
    return (BookmarkEntry) bean.getObject();
  }

  public void remove(BookmarkEntry entry)
  {
    BookmarkBean bean = new BookmarkBean();
    bean.setObject(entry);
    try
    {
      bean.remove();
    }
    catch(Exception e)
    {
      logger.error("Error remove bookmark entry:", e);
    }
  }

  public void remove(int id)
  {
    BookmarkBean bean = new BookmarkBean();
    BookmarkEntry entry = new BookmarkEntry();
    entry.setId(id);
    bean.setObject(entry);
    remove (entry);
  }

  public void update(BookmarkEntry entry)
  {
    BookmarkBean bean = new BookmarkBean();
    bean.setObject(entry);
    try
    {
      bean.store();
    }
    catch(Exception e)
    {
      logger.error("Error update bookmark entry:", e);
    }
  }

  public BookmarkEntry get(int id)
  {
    BookmarkBean bean = new BookmarkBean();
    BookmarkEntry entry = new BookmarkEntry();
    entry.setId(id);
    bean.setObject(entry);
    try
    {
      bean.load();
    }
    catch(Exception e)
    {
      logger.error("Error get bookmark entry:", e);
      return null;
    }
    return entry;
  }

  public int getTotalPages(int userid, int entriesPerPage, String VIEW_TYPE)
  {
    int pages = 1;
    try
    {
      String sql = null;
      int count = 0;

      if (VIEW_TYPE.compareTo(VT_SYSTEM) == 0)
      {
        sql = "userid=-1";
      }
      else if (VIEW_TYPE.compareTo(VT_MINE) == 0)
      {
        sql = "userid="+userid;
      }
      else
      {
        sql = "userid=-1 or userid="+userid;
      }

      BookmarkBean bean = new BookmarkBean();
      count = bean.getCount(sql);
      pages = ((count - 1) / entriesPerPage) + 1;
    }
    catch (Exception e)
    {
      logger.error("Error getTotalPages:", e);
    }
    return pages;
  }

  public Collection getBookmarks(String[] ids) throws Exception
  {
    ArrayList l = new ArrayList();
    if (ids==null || ids.length==0)
      return l;

    BookmarkBean bean = new BookmarkBean();
    for (int i=0; i<ids.length; i++)
    {
      BookmarkEntry entry = new BookmarkEntry();
      entry.setId(Integer.parseInt(ids[i]));
      bean.setObject(entry);
      try
      {
        bean.load();
        l.add(entry);
      }
      catch(Exception e)
      {
        //it's ok that the bookmark not exist
        logger.error("Error get bookmark entry id=" + ids[i], e);
      }
    }
    return l;
  }

  public Collection getBookmarks(int userid, String VIEW_TYPE, String SORTBY, boolean sortAsc, int from, int limit) throws Exception
  {
    if ( VT_MINE.compareTo(VIEW_TYPE) == 0)
    {
      return getUserBookmarks(userid, SORTBY, sortAsc, from, limit);
    }
    if ( VT_SYSTEM.compareTo(VIEW_TYPE) == 0)
    {
      return getSystemBookmarks(SORTBY, sortAsc, from, limit);
    }
    else
    {
      return getAllBookmarks(userid, SORTBY, sortAsc, from, limit);
    }
  }

  protected Collection getSystemBookmarks(String SORTBY, boolean sortAsc, int from, int limit) throws Exception
  {
    BookmarkBean bean = new BookmarkBean();
    StringBuffer sql = new StringBuffer();
    sql.append("userid =-1");
    if (bean.isValidFieldName(SORTBY))
    {
      sql.append(" order by ");
      sql.append(SORTBY);
      if (sortAsc)
        sql.append(" ASC ");
      else
        sql.append(" DESC ");
    }
    else
      logger.warn("Passing an invalid sortby fieldname:" + SORTBY);

    DBUtil.appendLimit(sql, from, limit);

    return bean.findBySql(sql.toString());
  }

  protected Collection getUserBookmarks(int userid, String SORTBY, boolean sortAsc, int from, int limit) throws Exception
  {
    BookmarkBean bean = new BookmarkBean();
    StringBuffer sql = new StringBuffer();
    sql.append("userid=");
    sql.append(userid);
    if (bean.isValidFieldName(SORTBY))
    {
      sql.append(" order by ");
      sql.append(SORTBY);
      if (sortAsc)
        sql.append(" ASC ");
      else
        sql.append(" DESC ");
    }
    else
      logger.warn("Passing an invalid sortby fieldname:" + SORTBY);

    DBUtil.appendLimit(sql, from, limit);

    return bean.findBySql(sql.toString());
  }

  protected Collection getAllBookmarks(int userid, String SORTBY, boolean sortAsc, int from, int limit) throws Exception
  {
    BookmarkBean bean = new BookmarkBean();
    StringBuffer sql = new StringBuffer();
    sql.append("userid=-1 or userid=");
    sql.append(userid);
    if (bean.isValidFieldName(SORTBY))
    {
      sql.append(" order by ");
      sql.append(SORTBY);
      if (sortAsc)
        sql.append(" ASC ");
      else
        sql.append(" DESC ");
    }
    else
      logger.warn("Passing an invalid sortby fieldname:" + SORTBY);

    DBUtil.appendLimit(sql, from, limit);

    return bean.findBySql(sql.toString());
  }

}